﻿using Engine;
using Game;
using GameEntitySystem;
using TemplatesDatabase;

public class Creaturecollection : Component, IUpdateable
{
    public UpdateOrder UpdateOrder
    {
        get
        {
            return 0;
        }
    }

    public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
    {
        base.Load(valuesDictionary, idToEntityMap);
        this.subsystemTerrain = base.Project.FindSubsystem<SubsystemTerrain>(true);
        this.subsystemPickables = base.Project.FindSubsystem<SubsystemPickables>(true);
        this.componentCreature = base.Entity.FindComponent<ComponentCreature>();
        this.flag = valuesDictionary.GetValue<bool>("flag");
    }

    public void Update(float dt)
    {
        if (!this.flag)
        {
            return;
        }
        ComponentInventoryBase componentInventoryBase = this.componentCreature.ComponentBody.Entity.FindComponent<ComponentInventoryBase>();
        foreach (Pickable pickable in this.subsystemPickables.Pickables)
        {
            TerrainChunk chunkAtCell = this.subsystemTerrain.Terrain.GetChunkAtCell(Terrain.ToCell(pickable.Position.X), Terrain.ToCell(pickable.Position.Z));
            if (componentInventoryBase != null && chunkAtCell != null && pickable.FlyToPosition == null && this.componentCreature.ComponentHealth.Health > 0f && ComponentInventoryBase.FindAcquireSlotForItem(componentInventoryBase, pickable.Value) >= 0)
            {
                Vector3 vector = this.componentCreature.ComponentBody.Position + new Vector3(0f, 0.8f, 0f);
                float num = (vector - pickable.Position).LengthSquared();

                if (num < 2f)
                {
                    pickable.ToRemove = true;
                    pickable.FlyToPosition = new Vector3?(vector);
                    pickable.Count = ComponentInventoryBase.AcquireItems(componentInventoryBase, pickable.Value, pickable.Count);
                }
            }
        }
    }

    public SubsystemTerrain subsystemTerrain;

    public SubsystemPickables subsystemPickables;

    public ComponentCreature componentCreature;

    public bool flag;
}